package com.qf;

import com.qf.dao.UserDao;
import com.qf.pojo.User;
import com.qf.pojo.UserExt;
import com.qf.proxy.FangDongService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试
 * @author lixu
 */
public class SpringJunit {

    /**
     * 入门程序
     * classpath:类路径
     *
     */
    @Test
    public void testSpring1(){

        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("spring-context.xml");

   /*     UserDao userDao = (UserDao) ApplicationContext.getBean("userDao");
        System.out.println(userDao.findAll());*/
        UserDao userDao1 = applicationContext.getBean(UserDao.class);
        System.out.println(userDao1.findAll());
    }
    /**
     * 入门程序
     * classpath:类路径
     *
     */
    @Test
    public void testSpring2(){

        //工厂
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("spring-context.xml");

        UserExt userExt = applicationContext.getBean(UserExt.class);
        System.out.println(userExt);

        //UserExt{id=123, password='666666',
        // sex='男', age=23,
        // bornDate=Mon Sep 05 00:00:00 CST 2022,
        // hobbys=[蓝球, 足球, 飞行],
        // phones=[138888888888, 136666666666, 139999999999],
        // names=[安迪, 老布, 长],
        // countries={zh=中国, ja=日本, hk=香港},
        // files={c盘=系统文件, d盘=学习文件, e盘=保密文件},
        // userList=[com.qf.pojo.User@16f7c8c1, com.qf.pojo.User@2f0a87b3, com.qf.pojo.User@319b92f3]}

    }

    /**
     *  Spring的容器 默认是单例模式   只有一份
     *      *
     *  request
     *  session
     *
     *  Application:整个项目 启动唯一个容器    Tomcat启动之后 在Tomcat服务中一个全局域
     *
     * classpath:类路径
     */
    @Test
    public void testSpring3(){

        //工厂
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("spring-context.xml");

        User user1= applicationContext.getBean(User.class);
        User user2= applicationContext.getBean(User.class);


        System.out.println(user1);
        System.out.println(user2);

    }

    /**
     * 生命周期
     *   单例模式：
     *   ===UserServiceImpl:创建对象==
     *   ===UserServiceImpl:构造器==
     *   ===UserServiceImpl:初始化==
     *   ===UserServiceImpl:销毁==
     *   多例模式：
     *   ===UserServiceImpl:创建对象==
     *   ===UserServiceImpl:构造器==
     *   ===UserServiceImpl:初始化==
     *                    JVM虚拟机垃圾回收  gc  堆==年轻代+老年代   full gc
     *
     */
    @Test
    public void testSpring4(){

        //工厂
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("spring-context.xml");
        applicationContext.getBean("userService");

        //
        ((ClassPathXmlApplicationContext)applicationContext).close();

    }

    /**
     * 代理 模式 之静态代理
     */
    @Test
    public void testSpring5(){

        //工厂
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("spring-context.xml");

        FangDongService proxyFangService = (FangDongService) applicationContext.getBean("proxyFangService");

        proxyFangService.zufang();

    }
}
